﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using LimitlessLightSwitch.Helper;
using LimitlessConnector;

namespace LimitlessLightSwitch
{
  public partial class SettingsPage : PhoneApplicationPage
  {
    private RGBWConnector m_Connector;

    public SettingsPage()
    {
      InitializeComponent();
      // Add an Application Bar that has a 'done' confirmation button and 
      // a 'cancel' button
      ApplicationBar = new ApplicationBar();
      ApplicationBar.IsMenuEnabled = true;
      ApplicationBar.IsVisible = true;
      ApplicationBar.Opacity = 1.0;

      ApplicationBarIconButton doneButton = new ApplicationBarIconButton(new Uri("/Images/confirm.png", UriKind.RelativeOrAbsolute));
      doneButton.Text = "confirm";
      doneButton.Click += new EventHandler(doneButton_Click);

      ApplicationBarIconButton cancelButton = new ApplicationBarIconButton(new Uri("/Images/close.png", UriKind.RelativeOrAbsolute));
      cancelButton.Text = "cancel";
      cancelButton.Click += new EventHandler(cancelButton_Click);

      ApplicationBar.Buttons.Add(doneButton);
      ApplicationBar.Buttons.Add(cancelButton);
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      base.OnNavigatedTo(e);
      m_Connector = (RGBWConnector)NavigationService.GetNavigationData();
    }

    void doneButton_Click(object sender, EventArgs e)
    {
      AppSettings settings = new AppSettings();
      settings.HostNameSetting = txtHostName.Text;
      settings.PortSetting = txtPort.Text;
      m_Connector.UpdateBridgeSettings(settings.HostNameSetting, settings.PortSetting);
      NavigationService.GoBack();
    }

    void cancelButton_Click(object sender, EventArgs e)
    {
      NavigationService.GoBack();
    }
  }
}